package cn.neo.support.iv.fresco.controller;

import android.graphics.drawable.Animatable;
import android.support.annotation.Nullable;
import android.view.ViewGroup;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;

import cn.neo.support.iv.fresco.utils.DensityUtil;

/**
 * <b>Name:</b>单张图片显示控制器<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b> 2016-12-26<br>
 * <b>Describe:</b>单张图片显示控制器<br>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b><br>
 */
public class SingleImageControllerListener extends BaseControllerListener<ImageInfo> {

    private final SimpleDraweeView draweeView;

    public SingleImageControllerListener(SimpleDraweeView draweeView) {
        this.draweeView = draweeView;
    }

    @Override
    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
        if (imageInfo == null || draweeView == null) {
            return;
        }

        ViewGroup.LayoutParams vp = draweeView.getLayoutParams();
        int maxWidth = DensityUtil.getDisplayWidth(draweeView.getContext()) - DensityUtil.dipToPixels(draweeView.getContext(), 16);
        int maxHeight = DensityUtil.getDisplayHeight(draweeView.getContext());
        int width = imageInfo.getWidth();
        int height = imageInfo.getHeight();

        //图片宽高比
        float aspectRatio = (float) width / (float) height;

        //第一次缩放
        width = maxWidth;
        height = (int) (width / aspectRatio);

        vp.height = height;
        vp.width = width;

        draweeView.requestLayout();
    }

}

